package com.repair.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.repair.entity.model.TeacherUser;
import com.repair.entity.model.User;
import com.repair.service.TeacherUserService;
import com.repair.util.MD5;
import com.repair.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 教师用户 前端控制器
 * </p>
 *
 * @author tianwenle
 * @since 2023-04-12
 */
@RestController
@RequestMapping("/user/teacher")
public class TeacherUserController {

    @Autowired
    private TeacherUserService teacherUserService;

    /**
     *
     * @param teacherNo 老师的编号
     * @param teacherName 老师的姓名
     * @param pageNum 第几页 1 2 3
     * @param pageSize 每一页显示多少条数据
     * @return
     */
    @GetMapping("/getTeachersInfo/{pageNum}/{pageSize}")
    public R getTeachersInfo(
            @RequestParam("teacherNo") String teacherNo,
            @RequestParam("teacherName") String teacherName,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ){

        Page<TeacherUser> page = new Page<>(pageNum,pageSize);
        // 这个方法查询完毕后 ，会把数据填充给page对象
        QueryWrapper<TeacherUser> wrapper = new QueryWrapper<>();
        // 编号查询
        /**
         * 参数1： boolean 满足进行拼接 不满足就不执行后续代码
         */
//        wrapper
//                .or(
//                        !"".equalsIgnoreCase(teacherNo) || teacherNo != null,
//                        s -> s.eq("teacher_no",teacherNo)
//                )
//                .or().eq().like();

        if (!"".equalsIgnoreCase(teacherNo) || teacherNo != null){
            wrapper.eq("teacher_no",teacherNo);
        }


        // 姓名查询
        if (!"".equalsIgnoreCase(teacherName) || teacherName != null){
            wrapper.or().like("teacher_name",teacherName);
        }

        wrapper.orderBy(true,false,"create_time");

        // 分页查询 执行完之后封装page
        teacherUserService.page(page,wrapper);
        // 获取到teacher的总数 4
        long total = page.getTotal();
        // 获取指定页数 指定数量的数据
        List<TeacherUser> list = page.getRecords();

        return R.ok().data("list",list).data("total",total);
    }


    @DeleteMapping("/deleteTeacherById/{id}")
    public R deleteTeacherById(@PathVariable("id") String id){
        teacherUserService.removeById(id);
        return R.ok();
    }

    @PostMapping("/addTeacher")
    public R addTeacher(@RequestBody TeacherUser teacherUser){
        // 设置默认密码
        teacherUser.setPassword(MD5.encrypt("123456"));
        // 保存教师信息
        teacherUserService.save(teacherUser);

        return R.ok();
    }

    @GetMapping("/getTeacherById/{id}")
    public R getTeacherById(@PathVariable("id") String id){
        TeacherUser teacherUser = teacherUserService.getById(id);
        return R.ok().data("teacher",teacherUser);
    }








}

